struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(nullptr) {}
};

class Solution {
public:

    //利用快慢指针模拟
    int kthToLast(ListNode* head, int k) {

        //注意考虑链表为空的情况
        if(!head)
        return 0;
        ListNode* slow = head,*fast = head;

        while(k--)  //fast先向前走k步
        {
            fast=fast->next;
        }
        while(fast)
        {
            slow=slow->next;
            fast = fast->next;
        }
        return slow->val;
    }
};
